﻿// 代码生成器 存档代码
namespace TowerDefence_Zombie.DataTemplates // namespace begin
{
    

    /// <summary>
    /// 存档代理
    /// </summary>
    public partial class SaveData
    {
        /// <summary>
        ///   底层存档接口
        /// </summary>
        GameHelper.Saving.SaveAdapter _api;

        /// <summary>
        ///   底层存档接口暴露
        /// </summary>
        public GameHelper.Saving.SaveAdapter adapter => _api;

        /// <summary>
        ///   获取所有存档键值
        /// </summary>
        public static readonly string[] AllKeys = new string[] {
            "GuideStep", // int
            "IsInit", // bool
            "PlacementAreaData", // TowerDefence_Zombie.PlacementAreaDataTemplate
            "PlayerData", // TowerDefence_Zombie.PlayerDataTemplate
            "SynthesisData", // TowerDefence_Zombie.SynthesisDataTemplate
        };

        /// <summary>
        ///   存档构造
        /// </summary>
        /// <param name="path">存档路径</param>
        /// <param name="password">AES密码, 密码为32个字符</param>
        /// <param name="suppressTypeError">禁止类型错误时抛出异常</param>
        public SaveData(string path, string password, bool suppressTypeError)
        {
            _api = new GameHelper.Saving.SaveAdapter(path, password, suppressTypeError);
            // this.Set = new SetProxy(_api);
            // this.Get = new GetProxy(_api);
            // this.GetOrDefault = new GetOrDefaultProxy(_api);
            // this.Query = new QueryProxy(_api);
            // this.Exists = new ExistsProxy(_api);
            // this.Remove = new RemoveProxy(_api);
        }
        
        public void ReplaceSetting(string path, string password)
        {
            _api.esFile.settings.path = path;
            _api.esFile.settings.encryptionPassword = password;
            _api.Flush();
        }

        /// <summary>
        /// 写入文件
        /// </summary>
        public void Flush()
        {
            // TODO:: 自定义类型写入
            _api.Flush();
        }

        /// <summary>
        /// 清理所有存档数值
        /// </summary>
        public void Clear()
        {
            // TODO:: cache字段状态清理
            _api.Clear();
        }

        /// <summary>
        /// 删除存档文件
        /// </summary>
        public void Delete()
        {
            // TODO:: cache字段状态清理
            _api.DeleteFile();
        }

        #region 存档 GuideStep

        int _mem_GuideStep;
        bool _loaded_GuideStep;


        public int GuideStep
        {
            get
            {
                return GuideStepOrDefault(default(int));
            }

            set
            {
                _api.Set<int>("GuideStep", value);
                _mem_GuideStep = value;
                _loaded_GuideStep = true;
            }
        }


        public int GuideStepOrDefault(int defaultValue)
        {
            if (!_loaded_GuideStep)
            {
                _mem_GuideStep = _api.Get<int>("GuideStep", defaultValue);
                _loaded_GuideStep = true;
            }
            return _mem_GuideStep;
        }


        public bool GuideStepQuery(out int value)
        {
            if (_loaded_GuideStep)
            {
                value = _mem_GuideStep;
                return true;
            }
            if (_api.TryGet<int>("GuideStep", out value))
            {
                _loaded_GuideStep = true;
                return true;
            }
            value = default(int);
            return false;
        }


        public bool GuideStepExists()
        {
            return _api.HasKey("GuideStep");
        }


        public void GuideStepRemove()
        {
            _loaded_GuideStep = false;
            _mem_GuideStep = default(int);
            _api.Remove("GuideStep");
        }

        #endregion

        #region 存档 IsInit

        bool _mem_IsInit;
        bool _loaded_IsInit;


        public bool IsInit
        {
            get
            {
                return IsInitOrDefault(default(bool));
            }

            set
            {
                _api.Set<bool>("IsInit", value);
                _mem_IsInit = value;
                _loaded_IsInit = true;
            }
        }


        public bool IsInitOrDefault(bool defaultValue)
        {
            if (!_loaded_IsInit)
            {
                _mem_IsInit = _api.Get<bool>("IsInit", defaultValue);
                _loaded_IsInit = true;
            }
            return _mem_IsInit;
        }


        public bool IsInitQuery(out bool value)
        {
            if (_loaded_IsInit)
            {
                value = _mem_IsInit;
                return true;
            }
            if (_api.TryGet<bool>("IsInit", out value))
            {
                _loaded_IsInit = true;
                return true;
            }
            value = default(bool);
            return false;
        }


        public bool IsInitExists()
        {
            return _api.HasKey("IsInit");
        }


        public void IsInitRemove()
        {
            _loaded_IsInit = false;
            _mem_IsInit = default(bool);
            _api.Remove("IsInit");
        }

        #endregion

        #region 存档 PlacementAreaData

        TowerDefence_Zombie.PlacementAreaDataTemplate _mem_PlacementAreaData;
        bool _loaded_PlacementAreaData;


        public TowerDefence_Zombie.PlacementAreaDataTemplate PlacementAreaData
        {
            get
            {
                return PlacementAreaDataOrDefault(default(TowerDefence_Zombie.PlacementAreaDataTemplate));
            }

            set
            {
                _api.Set<TowerDefence_Zombie.PlacementAreaDataTemplate>("PlacementAreaData", value);
                _mem_PlacementAreaData = value;
                _loaded_PlacementAreaData = true;
            }
        }


        public TowerDefence_Zombie.PlacementAreaDataTemplate PlacementAreaDataOrDefault(TowerDefence_Zombie.PlacementAreaDataTemplate defaultValue)
        {
            if (!_loaded_PlacementAreaData)
            {
                _mem_PlacementAreaData = _api.Get<TowerDefence_Zombie.PlacementAreaDataTemplate>("PlacementAreaData", defaultValue);
                _loaded_PlacementAreaData = true;
            }
            return _mem_PlacementAreaData;
        }


        public bool PlacementAreaDataQuery(out TowerDefence_Zombie.PlacementAreaDataTemplate value)
        {
            if (_loaded_PlacementAreaData)
            {
                value = _mem_PlacementAreaData;
                return true;
            }
            if (_api.TryGet<TowerDefence_Zombie.PlacementAreaDataTemplate>("PlacementAreaData", out value))
            {
                _loaded_PlacementAreaData = true;
                return true;
            }
            value = default(TowerDefence_Zombie.PlacementAreaDataTemplate);
            return false;
        }


        public bool PlacementAreaDataExists()
        {
            return _api.HasKey("PlacementAreaData");
        }


        public void PlacementAreaDataRemove()
        {
            _loaded_PlacementAreaData = false;
            _mem_PlacementAreaData = default(TowerDefence_Zombie.PlacementAreaDataTemplate);
            _api.Remove("PlacementAreaData");
        }

        #endregion

        #region 存档 PlayerData

        TowerDefence_Zombie.PlayerDataTemplate _mem_PlayerData;
        bool _loaded_PlayerData;


        public TowerDefence_Zombie.PlayerDataTemplate PlayerData
        {
            get
            {
                return PlayerDataOrDefault(default(TowerDefence_Zombie.PlayerDataTemplate));
            }

            set
            {
                _api.Set<TowerDefence_Zombie.PlayerDataTemplate>("PlayerData", value);
                _mem_PlayerData = value;
                _loaded_PlayerData = true;
            }
        }


        public TowerDefence_Zombie.PlayerDataTemplate PlayerDataOrDefault(TowerDefence_Zombie.PlayerDataTemplate defaultValue)
        {
            if (!_loaded_PlayerData)
            {
                _mem_PlayerData = _api.Get<TowerDefence_Zombie.PlayerDataTemplate>("PlayerData", defaultValue);
                _loaded_PlayerData = true;
            }
            return _mem_PlayerData;
        }


        public bool PlayerDataQuery(out TowerDefence_Zombie.PlayerDataTemplate value)
        {
            if (_loaded_PlayerData)
            {
                value = _mem_PlayerData;
                return true;
            }
            if (_api.TryGet<TowerDefence_Zombie.PlayerDataTemplate>("PlayerData", out value))
            {
                _loaded_PlayerData = true;
                return true;
            }
            value = default(TowerDefence_Zombie.PlayerDataTemplate);
            return false;
        }


        public bool PlayerDataExists()
        {
            return _api.HasKey("PlayerData");
        }


        public void PlayerDataRemove()
        {
            _loaded_PlayerData = false;
            _mem_PlayerData = default(TowerDefence_Zombie.PlayerDataTemplate);
            _api.Remove("PlayerData");
        }

        #endregion

        #region 存档 SynthesisData

        TowerDefence_Zombie.SynthesisDataTemplate _mem_SynthesisData;
        bool _loaded_SynthesisData;


        public TowerDefence_Zombie.SynthesisDataTemplate SynthesisData
        {
            get
            {
                return SynthesisDataOrDefault(default(TowerDefence_Zombie.SynthesisDataTemplate));
            }

            set
            {
                _api.Set<TowerDefence_Zombie.SynthesisDataTemplate>("SynthesisData", value);
                _mem_SynthesisData = value;
                _loaded_SynthesisData = true;
            }
        }


        public TowerDefence_Zombie.SynthesisDataTemplate SynthesisDataOrDefault(TowerDefence_Zombie.SynthesisDataTemplate defaultValue)
        {
            if (!_loaded_SynthesisData)
            {
                _mem_SynthesisData = _api.Get<TowerDefence_Zombie.SynthesisDataTemplate>("SynthesisData", defaultValue);
                _loaded_SynthesisData = true;
            }
            return _mem_SynthesisData;
        }


        public bool SynthesisDataQuery(out TowerDefence_Zombie.SynthesisDataTemplate value)
        {
            if (_loaded_SynthesisData)
            {
                value = _mem_SynthesisData;
                return true;
            }
            if (_api.TryGet<TowerDefence_Zombie.SynthesisDataTemplate>("SynthesisData", out value))
            {
                _loaded_SynthesisData = true;
                return true;
            }
            value = default(TowerDefence_Zombie.SynthesisDataTemplate);
            return false;
        }


        public bool SynthesisDataExists()
        {
            return _api.HasKey("SynthesisData");
        }


        public void SynthesisDataRemove()
        {
            _loaded_SynthesisData = false;
            _mem_SynthesisData = default(TowerDefence_Zombie.SynthesisDataTemplate);
            _api.Remove("SynthesisData");
        }

        #endregion


// todo

//        /// <summary>
//        /// Get接口
//        /// </summary>
//        public class SetProxy
//        {
//            GameHelper.Saving.SaveAdapter _api;
//            public SetProxy(GameHelper.Saving.SaveAdapter api) { this._api = api; }
//// todo
//        }
//
//        /// <summary>
//        /// Set接口
//        /// </summary>
//        public class GetProxy
//        {
//            GameHelper.Saving.SaveAdapter _api;
//
//            public GetProxy(GameHelper.Saving.SaveAdapter api) { this._api = api; }
//// todo
//        }
//
//        /// <summary>
//        /// Get接口, 键值不存在时返回default
//        /// </summary>
//        public class GetOrDefaultProxy
//        {
//            GameHelper.Saving.SaveAdapter _api;
//            public GetOrDefaultProxy(GameHelper.Saving.SaveAdapter api) { this._api = api; }
//// todo
//        }
//
//        /// <summary>
//        /// Get接口, 键值不存在时返回false
//        /// </summary>
//        public class QueryProxy
//        {
//            GameHelper.Saving.SaveAdapter _api;
//            public QueryProxy(GameHelper.Saving.SaveAdapter api) { this._api = api; }
//// todo
//        }
//
//        /// <summary>
//        /// 查询键值是否存在
//        /// </summary>
//        public class ExistsProxy
//        {
//            GameHelper.Saving.SaveAdapter _api;
//            public ExistsProxy(GameHelper.Saving.SaveAdapter api) { this._api = api; }
//// todo
//        }
//
//        /// <summary>
//        /// 存档值移除接口
//        /// </summary>
//        public class RemoveProxy
//        {
//            GameHelper.Saving.SaveAdapter _api;
//            public RemoveProxy(GameHelper.Saving.SaveAdapter api) { this._api = api; }
//// todo
//        }
//
//        public readonly SetProxy Set;
//        public readonly GetProxy Get;
//        public readonly GetOrDefaultProxy GetOrDefault;
//        public readonly QueryProxy Query;
//        public readonly ExistsProxy Exists;
//        public readonly RemoveProxy Remove;

   }
} // namespace end

// TODO:: 记录代码生成数据，用于检测Key对应的数据类型变更，key删除和新增问题
// +META-BEGIN
// #META_DATA#
// +META-END
